package cn.zymall.app.controller.page;

import cn.zymall.common.model.Admin;
import cn.zymall.common.model.Order;
import cn.zymall.common.model.User;
import cn.zymall.common.utils.amqp.AMQPField;
import cn.zymall.common.utils.amqp.AMQPUtil;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.*;

/**
 * @author xuxianda
 * @Description: 用户行为与队列交互控制器
 * @date 2020/8/18 16:00
 * @Copyright
 * @return
 **/
@RestController
@RequestMapping("/queues")
public class UserActionQueueController {
    /**
     * 需要在application.properties中配置rabbitmq
     */
    @Autowired
    AMQPUtil amqpUtil;

    /**
     * 用户注册队列发送消息方法
     * @param user
     * @return
     */
    @PostMapping("/userRegister")
    public boolean userRegister(@RequestBody User user) {
        try {
            System.out.println(user);
            Gson gson = new Gson();
            String s = gson.toJson(user);
            amqpUtil.sendMsg(AMQPField.USER_REGISTER, s);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 后台用户注册队列发送消息方法
     * @param admin
     * @return
     */
    @PostMapping("/adminRegister")
    public boolean adminRegister(@RequestBody Admin admin) {
        try {
            System.out.println(admin);
            Gson gson = new Gson();
            String s = gson.toJson(admin);
            amqpUtil.sendMsg(AMQPField.ADMIN_REGISTER, s);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 订单下单队列发送消息方法
     * @param order
     * @return
     */
    @PostMapping("/orders")
    public boolean userRegister(@RequestBody Order order) {
        try {
            System.out.println(order);
            Gson gson = new Gson();
            String s = gson.toJson(order);
            amqpUtil.sendMsg(AMQPField.ORDERS, s);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
